package com.example.customview.view

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.util.AttributeSet
import android.view.View
import com.example.customview.R
import com.sample.base.utils.dp

@SuppressLint("NewApi")
class SportView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {
    companion object {
        private val RADIUS = 160.dp
        private val RING_WIDTH = 20.dp
    }

    private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val fontMetrics = Paint.FontMetrics()
    private val textBound = Rect()

    override fun onDraw(canvas: Canvas) {
        // 绘制底部圆环
        paint.style = Paint.Style.STROKE
        paint.color = resources.getColor(R.color.colorGrey, resources.newTheme())
        paint.strokeWidth = RING_WIDTH.toFloat()
        canvas.drawCircle(width / 2f, height / 2f, RADIUS.toFloat(), paint)
        //绘制进度条
        paint.color = resources.getColor(R.color.colorJINLIANHUACHENG, resources.newTheme())
        paint.strokeCap = Paint.Cap.ROUND
        canvas.drawArc(
            width / 2f - RADIUS, height / 2f - RADIUS,
            width / 2f + RADIUS, height / 2f + RADIUS,
            -90f, 234f, false, paint
        )

        // 绘制中间的文字
        paint.style = Paint.Style.FILL
        paint.color = resources.getColor(R.color.colorJINLIANHUACHENG, resources.newTheme())
        paint.textSize = 100.dp.toFloat()
        paint.textAlign = Paint.Align.CENTER
        paint.getFontMetrics(fontMetrics)
        canvas.drawText(
            "bbq",
            width / 2f,
            height / 2f - (fontMetrics.ascent + fontMetrics.descent) / 2f,
            paint
        )

        // 绘制右下对其的文字
        val rightText = "right"
        paint.style = Paint.Style.FILL
        paint.textSize = 88.dp.toFloat()
        paint.textAlign = Paint.Align.RIGHT
        paint.getTextBounds(rightText, 0, rightText.length, textBound)
        canvas.drawText(
            rightText,
            (width + textBound.left).toFloat(),
            (height - textBound.bottom).toFloat(),
            paint
        )

        //绘制左上对其的文字
        val leftText = "left"
        paint.textAlign = Paint.Align.LEFT
        paint.getTextBounds(leftText, 0, leftText.length, textBound)
        canvas.drawText(
            leftText,
            (-textBound.left).toFloat(),
            (-textBound.top).toFloat(),
            paint
        )
    }
}